package org.movsim.simulator.pslogger;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.movsim.simulator.B5Constants.B5_PSYCOLOGICAL_FACTORS;
import org.movsim.simulator.B5Constants.INPUT_FROM_ENVIRONMENT;
import org.movsim.simulator.roadnetwork.RoadSegment;
import org.movsim.simulator.vehicles.Environment;
import org.movsim.simulator.vehicles.PSEnabledVehicle;
import org.movsim.simulator.vehicles.psdecisioncriteria.PSBasedDecisionCriteria;

public class PSLogger {
    
    public static void initilizeFile(File f) {

        if (f.exists()) {
            f.delete();
        }
        try {
            f.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
            dos.writeChars("DT");
            dos.writeChar(',');
            dos.writeChars("ST");
            dos.writeChar(',');
            dos.writeChars("IC");
            dos.writeChar(',');
            for (B5_PSYCOLOGICAL_FACTORS p : B5_PSYCOLOGICAL_FACTORS.values()) {
                dos.writeChars(p.toString());
                dos.writeChar(',');
            }
            for (INPUT_FROM_ENVIRONMENT i : INPUT_FROM_ENVIRONMENT.values()) {
                dos.writeChars(i.toString());
                dos.writeChar(',');
            }
            dos.writeChars("" + System.getProperty("line.separator"));
            dos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void logPsyStatesAndInputEnvoirnment(double dt, double simulationTime, long iterationCount,
            PSEnabledVehicle pv, RoadSegment roadSegment) {
        double[] pstates = PSBasedDecisionCriteria.scalePsyclogicalStates(pv.getCumulativePsyStates());
        Environment env = pv.getEnv();
        File f = pv.f;
        try {
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(f, true));
            dos.writeChars(Double.toString(dt));
            dos.writeChar(',');
            dos.writeChars(Double.toString(simulationTime));
            dos.writeChar(',');
            dos.writeChars(Double.toString(iterationCount));
            dos.writeChar(',');

            for (B5_PSYCOLOGICAL_FACTORS p : B5_PSYCOLOGICAL_FACTORS.values()) {
                dos.writeChars(Double.toString(pstates[p.ordinal()]));
                dos.writeChar(',');
            }

            for (INPUT_FROM_ENVIRONMENT i : INPUT_FROM_ENVIRONMENT.values()) {
                double v = 0;
                v = env.getDegreeOfEnvironment(i, roadSegment);
                dos.writeChars(Double.toString(v));
                dos.writeChar(',');
            }
            dos.writeChars("" + System.getProperty("line.separator"));
            dos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}
